<template>
	<view class="">
		<view class="bg-w pd15">
			<u--form labelPosition="left" :model="model1" :rules="rules" ref="uForm" labelWidth="180rpx">
				<u-form-item label="账户名称" prop="accName" ref="item1" :required="true">
					<u--input v-model="model1.accName" border="surround" placeholder="请输入账户名称"></u--input>
				</u-form-item>
				<u-form-item label="账户编号" prop="accCode" ref="item1" :required="true">
					<u--input v-model="model1.accCode" border="surround" placeholder="请输入账户编号" disabled></u--input>
				</u-form-item>
				<u-form-item label="期初余额" prop="openingBalance" ref="item1">
					<u--input v-model="model1.openingBalance" border="surround" clearable
						placeholder="请输入期初余额"></u--input>
				</u-form-item>
				<u-form-item label="开账时间" prop="billingTime" ref="item1" @click="show=true">
					<u--input v-model="model1.billingTime" border="surround" placeholder="请选择开账时间" disabledColor="#fff" disabled></u--input>
				</u-form-item>
				<u-form-item label="备注" prop="remark" ref="item1">
					<u--textarea v-model="model1.remark" border="surround" placeholder="请输入备注"></u--textarea>
				</u-form-item>
			</u--form>
		</view>
		<view style="height: 200rpx;"></view>
		<view class="footer">
			<u-button type="primary" text="编辑" @click="baseAccountEditClick" v-if="model1.id"></u-button>
			<u-button type="primary" text="保存" @click="baseAccountClick" v-else></u-button>
		</view>
		<u-datetime-picker :show="show" v-model="value1" mode="date" @confirm="confirm"></u-datetime-picker>
	</view>
</template>

<script>
	import {
		id
	} from 'element-plus/es/locale';
	export default {
		data() {
			return {
				model1: {
					id: '',
					accCode: "",
					accName: "",
					billingTime: "",
					openingBalance: '',
					remark: ""
				},
				rules: {
					'accName': {
						type: 'string',
						required: true,
						message: '请输入账户名称',
						trigger: ['blur', 'change']
					},
					'accCode': {
						type: 'string',
						required: true,
						message: '请输入账户编号',
						trigger: ['blur', 'change']
					}
				},
				pageNo: 1,
				pageSize: 100,
				show: false,
				value1: Number(new Date()),
			}
		},
		onReady() {
			//如果需要兼容微信小程序，并且校验规则中含有方法等，只能通过setRules方法设置规则。
			this.$refs.uForm.setRules(this.rules)
		},
		/**
		 * 生命周期函数--监听页面加载
		 */
		onLoad(options) {
			if (!this.$u.test.isEmpty(options.item)) {
				const item = JSON.parse(decodeURIComponent(options.item));
				console.log(item)
				this.model1 = {
					id: item.id,
					accCode: item.accCode,
					accName: item.accName,
					billingTime: item.billingTime,
					openingBalance: item.openingBalance,
					remark: item.remark
				}
			} else {
				this.fin_zh_bill_no()
			}

		},
		onShow() {
			// this.isLogin = checkLogin()
			// if (!this.isLogin) {
			// 	this.isloginBind()
			// 	return false
			// }


		},
		methods: {
			confirm(e){
				this.model1.billingTime = this.$u.timeFormat(e.value, 'yyyy-mm-dd');
				this.show = false
			},
			//fin_zh_bill_no账户编号
			fin_zh_bill_no() {
				this.$apis.fin_zh_bill_no().then(res => {
					this.model1.accCode = res.result
				})
			},
			baseAccountClick() {
				let form = {
					accCode: this.model1.accCode,
					accName: this.model1.accName,
					billingTime: this.model1.billingTime,
					openingBalance: this.model1.openingBalance,
					remark: this.model1.remark
				}
				console.log(form)
				this.$refs.uForm.validate().then(res => {
					this.$apis.baseAccountAdd(form).then(res => {
						if (res.code == 200) {
							this.$u.toast(res.result)
							setTimeout(() => {
								uni.navigateBack()
							}, 1000);
						}
					})
				}).catch(errors => {})
			},
			baseAccountEditClick() {
				let form = {
					id: this.model1.id,
					accCode: this.model1.accCode,
					accName: this.model1.accName,
					billingTime: this.model1.billingTime,
					openingBalance: this.model1.openingBalance,
					remark: this.model1.remark
				}
				console.log(form)
				this.$refs.uForm.validate().then(res => {
					this.$apis.baseAccountEdit(form).then(res => {
						if (res.code == 200) {
							this.$u.toast(res.result)
							setTimeout(() => {
								uni.navigateBack()
							}, 1000);
						}
					})
				}).catch(errors => {})
			}
		},


	}
</script>
<style>
	page {
		background-color: #F5F5F5;
	}
</style>
<style lang="scss" scoped>
	.tagoul {
		display: flex;
		background-color: #f5f5f5;
	}

	.tagclass {
		height: 400px;
		overflow: scroll;
		list-style: none;
	}

	.icon {
		border: 1px solid #E0E0E0;
		padding: 8rpx 20rpx;
		border-radius: 10rpx;
	}

	.add {
		background-color: #3247D3;
		padding: 8rpx 20rpx;
		border-radius: 10rpx;
		color: #fff;
	}

	.cl-status {
		color: #6A6A6A;
	}

	.pore {
		position: relative;
	}

	.popBox {
		position: relative;

		.btns {
			position: absolute;
			bottom: 30rpx;
			right: 30rpx;
			left: 30rpx;

			.confirm {
				padding: 8rpx 30rpx;
				border-radius: 10rpx;
				background-color: #3247D3;
				color: #fff;
			}
		}
	}

	.border-top {
		border-top: 1px solid #F8F8F8;
	}

	.number {
		background-color: #3247D3;
		color: #fff;
		font-size: 22rpx;
		padding: 4rpx 10rpx;
		border-radius: 8rpx;
	}

	.footer {
		position: fixed;
		bottom: 0;
		background-color: #fff;
		width: 100%;
		padding: 30rpx;
		z-index: 999;
	}

	.cl-h1 {
		color: #6fbcff
	}
</style>